package com.nuotai.pms.shiro.service;

import com.nuotai.pms.common.entity.*;
import com.nuotai.pms.common.utils.CommonUtils;
import com.nuotai.pms.common.utils.MD5Utils;
import com.nuotai.pms.common.utils.ShiroUtils;
import com.nuotai.pms.shiro.dao.TaskMapper;
import com.nuotai.pms.shiro.manager.SysUserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 任务管理
 * @author yyf
 */
@Service("taskService")
public class TaskService {

	@Autowired
	private TaskMapper taskMapper;
	
	public List<TaskEntity> queryTaskList(Map<String, Object> params) {
		List<TaskEntity> taskList = taskMapper.queryTaskList(params);
		return taskList;
	}

	//添加活动
	public int addTask(Map<String,Object> addMap){
		SysUserEntity user = ShiroUtils.getUserEntity();
		String id = ShiroUtils.getUid();//获取主键
		String content = addMap.get("content").toString();
		String date1Str = addMap.get("date1").toString();
		String date2Str = addMap.get("date2").toString();
		String regionNo = addMap.get("regionId").toString();
		String taskType = addMap.get("taskType").toString();

		String taskIntegralStr = addMap.get("taskIntegral").toString();//积分
		String responsibilityUserIdStr = addMap.get("responsibilityUserId").toString();//负责人
		String relationshipParentTaskId = addMap.get("relationshipParentTaskId").toString();//上级关联任务

		int taskIntegral = 0;
		if(!"".equals(taskIntegralStr)){
			taskIntegral = Integer.parseInt(taskIntegralStr);
		}
		Long responsibilityUserId = new Long(0);
		if(!"".equals(responsibilityUserIdStr)){
			responsibilityUserId = Long.parseLong(responsibilityUserIdStr);
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1 = new Date();
		Date date2 = new Date();
		try {
			date1 = sdf.parse(date1Str);
			date2 = sdf.parse(date2Str);
		}catch (ParseException e){
			e.printStackTrace();
		}
		TaskEntity taskEntity = new TaskEntity();
		taskEntity.setId(id);
		taskEntity.setContent(content);
		taskEntity.setStartTime(date1);
		taskEntity.setEndTime(date2);
		taskEntity.setRegionNo(regionNo);
		taskEntity.setTaskType(taskType);
		taskEntity.setCreateUserId(user.getUserId());
		taskEntity.setCreateUserName(user.getUsername());
		taskEntity.setTaskStatus("0");//0 待处理，1 处理中， 2 处理结束，3 终结
		taskEntity.setTaskIntegral(taskIntegral);
		taskEntity.setResponsibilityUserId(responsibilityUserId);
		taskEntity.setRelationshipParentTaskId(relationshipParentTaskId);
		/*Map<String,Object> taskEntityMap = new HashMap<String,Object>();
		taskEntityMap.put("id",id);
		taskEntityMap.put("content",content);
		taskEntityMap.put("startTime",date1);
		taskEntityMap.put("endTime",date2);
		taskEntityMap.put("createUserId",user.getUserId());
		taskEntityMap.put("createUserName",user.getUsername());
		taskEntityMap.put("taskType",taskType);
		taskEntityMap.put("regionNo",regionNo);
		taskEntityMap.put("taskStatus","0");//0 待处理，1 处理中， 2 处理结束，3 终结
		taskEntityMap.put("taskIntegral",taskIntegral);
		taskEntityMap.put("responsibilityUserId",responsibilityUserId);
		taskEntityMap.put("relationshipParentTaskId",relationshipParentTaskId);*/
		return taskMapper.addTask(taskEntity);
	}
}
